﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;
using System.IO;

namespace Gundam_2._0_Alpha.src
{
    public class CreateNewProfile: IAction
    {
        public void DoAction(GameController gc)
        {
            gc.P.Name = Input.TextReadAsASCII();

            if (gc.P.Name == null || gc.P.Name == "" || gc.P.Name.Contains(" "))
            {
                IAction tempAction = new CreateProfileAction();
                gc.GameStages.Peek().StageAction.Remove(tempAction);
                gc.GameStages.Peek().StageAction.Add(tempAction);
                
                return;
            }

            UpdateSaveFiles(gc.P.Name);

            IAction createMissionSelect = new ICreateMissionSelect(gc.P.Name);

            gc.GameStages.Pop();

            createMissionSelect.DoAction(gc);
        }

        private void UpdateSaveFiles(string name)
        {
            string[] saveFile = System.IO.File.ReadAllLines((Resources.PathToResource("save\\save.txt")));

            for (int i = 0; i < saveFile.Length; i++)
            {
                if (saveFile[i].Contains("<Empty>"))
                {
                    saveFile[i] = name;
                    break;
                }
            }

            StreamWriter newProfile = new StreamWriter(Resources.PathToResource("save\\" + name + ".txt"));
            newProfile.WriteLine("1");
            newProfile.Close();

            StreamWriter updateSave = new StreamWriter(Resources.PathToResource("save\\save.txt"));
            for (int i = 0; i < saveFile.Length; i++)
                    updateSave.WriteLine(saveFile[i]);
            updateSave.Close(); 
        } 
       
    }
}
